<?php

# PMB Online
# Aplikasi PMB Online untuk Pendaftaran Calon Mahasiswa di PT/PTS Indonesia
# 
# version 1.0
# class jurusan
# file models/jurusan.php
# created Jan 22, 2013 10:33:09 AM
# 
# (c)2012, arif.laksito@gmail.com

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Jurusan extends SI_Model {

    var $data = array();

    public function __construct() {
        parent::__construct();
    }
    
    public function get_by_program($p=''){                
        $this->db->where('jenjang',$p);
        $que = $this->db->get('jurusan');
        
        return $que->result_array();
    }
    
    public function get_all(){        
        $this->db->order_by('jenjang');
        $que = $this->db->get('jurusan');        
        
        return $que->result_array();
    }
    
    public function get_spp(){
        $this->db->join('tha','tha.id_tha = tha_jur.id_tha');
        $this->db->join('jurusan','jurusan.id_jurusan = tha_jur.id_jurusan');
        $this->db->where('tha.status',1);
        $que = $this->db->get('tha_jur');
        
        return $que->result_array();
    }
    
    public function get_by_id($id){        
        $this->db->where('jurusan.id_jurusan',$id);
        $que = $this->db->get('jurusan');
        
        return $que->row_array();
    }
    
    public function add($data = array()){
        $rules = array(            
            array('field' => 'nama_jurusan', 'label' => 'Nama Jurusan', 'rules' => 'required|trim'),
            array('field' => 'jenjang', 'label' => 'Jenjang Pendidikan', 'rules' => 'required|trim')
        );

        if (!$this->set_field($rules)) {
            $this->umsg = $this->msg;
            return false;
        }
        
        $this->load->model('th_pmb');
        $tha = $this->th_pmb->get_tha_aktif();
        
        $jur['nama_jurusan'] = $data['nama_jurusan'];
        $jur['jenjang'] = $data['jenjang'];
        $jur_th['id_tha'] = $tha['id_tha'];
        
        if ($this->db->insert('jurusan', $jur)) {            
            $this->umsg = 'Data Jurusan berhasil ditambahkan';
            return true;
        } else {
            $this->umsg = 'Data Jurusan gagal ditambahkan';
            return false;
        }
    }
    
    public function add_spp($data = array()){
        $rules = array(                        
            array('field' => 'spp_tetap', 'label' => 'Biaya SPP Tetap', 'rules' => 'numeric|trim'),  
            array('field' => 'spp_var', 'label' => 'Biaya SPP Variabel', 'rules' => 'numeric|trim')            
        );
        
        if (!$this->set_field($rules)) {
            $this->umsg = $this->msg;
            return false;
        }
        
        $this->load->model('th_pmb');
        $tha = $this->th_pmb->get_tha_aktif();
        $data['id_tha'] = $tha['id_tha'];
        
        if ($this->db->insert('tha_jur', $data)) {
            
            $this->umsg = 'Data Biaya SPP berhasil ditambahkan';
            return true;
        } else {
            $this->umsg = 'Data Biaya SPP gagal ditambahkan';
            return false;
        }
    }
    
    public function edit($id, $data = array()){
        $rules = array(            
            array('field' => 'nama_jurusan', 'label' => 'Nama Jurusan', 'rules' => 'required|trim'),
            array('field' => 'jenjang', 'label' => 'Jenjang Pendidikan', 'rules' => 'required|trim')
        );

        if (!$this->set_field($rules)) {
            $this->umsg = $this->msg;
            return false;
        }
        
        $this->load->model('th_pmb');
        $tha = $this->th_pmb->get_tha_aktif();
        
        $jur['nama_jurusan'] = $data['nama_jurusan'];
        $jur['jenjang'] = $data['jenjang'];
                
        $this->db->where('id_jurusan',$id);
        if ($this->db->update('jurusan', $jur)) {            
            
            $this->umsg = 'Data Jurusan berhasil diedit';
            return true;
        } else {
            $this->umsg = 'Data Jurusan gagal diedit';
            return false;
        }
    }
    
    public function delete($id){
        
        $this->load->model('th_pmb');
        $tha = $this->th_pmb->get_tha_aktif();
        
        $this->db->where('id_tha',$tha['id_tha']);
        $this->db->where('id_jurusan',$id);
        $this->db->delete('tha_jur');
        
        $this->db->where('id_jurusan',$id);
        if($this->db->delete('jurusan')){                                         
            return true;
        }else{
            return false;
        }
    }
    
}    